package com.yztc.yidoutang.http;

import android.support.annotation.NonNull;

import com.google.gson.GsonBuilder;
import com.yztc.yidoutang.share.SharedURL;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 使用Retrofit访问网络获取数据的工具类
 */
public class HttpHelper {
    // 单例的HttpHelper对象
    private static volatile HttpHelper singleton;

    // 私有化构造方法，防止外部访问
    private HttpHelper() {
    }

    // 获取HttpHelper单例的方法
    public static HttpHelper getInstance() {
        if (singleton == null) {
            synchronized (HttpHelper.class) {
                if (singleton == null) {
                    singleton = new HttpHelper();
                }
            }
        }
        return singleton;
    }

    @NonNull
    public Retrofit getRetrofit() {
        // 使用Retrofit访问网络获取数据
        return new Retrofit
                .Builder()
                //配置基础的url
                .baseUrl(SharedURL.BASE_URL)
                //配置提交或者返回的参数的造型方式为gson
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
                //返回值可以使用Obserable
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                //使用https时需要配置
                .client(new OkHttpClient())
                .build();
    }
}